/**
 * 权限控制工具函数
 */

import { useUserStore } from '@/stores/user'

/**
 * 检查用户是否有指定权限
 * @param {string|Array} permission - 权限代码或权限代码数组
 * @returns {boolean} - 是否有权限
 */
export function hasPermission(permission) {
  const userStore = useUserStore()
  const userRoles = userStore.userRoles
  
  // 如果是超级管理员，拥有所有权限
  if (userRoles.includes('admin')) {
    return true
  }
  
  // 模拟权限映射表
  const rolePermissions = {
    admin: ['*'], // 超级管理员拥有所有权限
    editor: [
      'user:view',
      'user:create',
      'user:edit',
      'content:view',
      'content:create',
      'content:edit',
      'order:view'
    ],
    normal: [
      'user:view',
      'content:view',
      'order:view'
    ],
    viewer: [
      'user:view',
      'content:view'
    ]
  }
  
  // 获取用户所有权限
  const userPermissions = []
  userRoles.forEach(role => {
    const permissions = rolePermissions[role] || []
    userPermissions.push(...permissions)
  })
  
  // 如果有通配符权限，返回 true
  if (userPermissions.includes('*')) {
    return true
  }
  
  // 检查单个权限
  if (typeof permission === 'string') {
    return userPermissions.includes(permission)
  }
  
  // 检查权限数组（需要拥有所有权限）
  if (Array.isArray(permission)) {
    return permission.every(p => userPermissions.includes(p))
  }
  
  return false
}

/**
 * 检查用户是否有任意一个权限
 * @param {Array} permissions - 权限代码数组
 * @returns {boolean} - 是否有任意一个权限
 */
export function hasAnyPermission(permissions) {
  if (!Array.isArray(permissions)) {
    return false
  }
  
  return permissions.some(permission => hasPermission(permission))
}

/**
 * 检查用户是否有指定角色
 * @param {string|Array} role - 角色代码或角色代码数组
 * @returns {boolean} - 是否有角色
 */
export function hasRole(role) {
  const userStore = useUserStore()
  const userRoles = userStore.userRoles
  
  if (typeof role === 'string') {
    return userRoles.includes(role)
  }
  
  if (Array.isArray(role)) {
    return role.some(r => userRoles.includes(r))
  }
  
  return false
}

/**
 * 权限指令
 * 用法：v-permission="'user:create'"
 */
export const permissionDirective = {
  mounted(el, binding) {
    const permission = binding.value
    if (!hasPermission(permission)) {
      el.style.display = 'none'
    }
  },
  updated(el, binding) {
    const permission = binding.value
    if (!hasPermission(permission)) {
      el.style.display = 'none'
    } else {
      el.style.display = ''
    }
  }
}

/**
 * 角色指令
 * 用法：v-role="'admin'"
 */
export const roleDirective = {
  mounted(el, binding) {
    const role = binding.value
    if (!hasRole(role)) {
      el.style.display = 'none'
    }
  },
  updated(el, binding) {
    const role = binding.value
    if (!hasRole(role)) {
      el.style.display = 'none'
    } else {
      el.style.display = ''
    }
  }
}

// 用户管理模块权限常量
export const USER_PERMISSIONS = {
  VIEW: 'user:view',
  CREATE: 'user:create',
  EDIT: 'user:edit',
  DELETE: 'user:delete',
  RESET_PASSWORD: 'user:reset_password',
  TOGGLE_STATUS: 'user:toggle_status',
  BATCH_DELETE: 'user:batch_delete'
}

// 内容管理模块权限常量
export const CONTENT_PERMISSIONS = {
  VIEW: 'content:view',
  CREATE: 'content:create',
  EDIT: 'content:edit',
  DELETE: 'content:delete',
  PUBLISH: 'content:publish'
}

// 订单管理模块权限常量
export const ORDER_PERMISSIONS = {
  VIEW: 'order:view',
  EDIT: 'order:edit',
  CANCEL: 'order:cancel',
  REFUND: 'order:refund'
}
